using System.Collections;
using System.Collections.Generic;
using Mono.Cecil.Cil;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Player : MonoBehaviour
{
    private Rigidbody2D rb;
    private Vector2 movement;
    private Animator animator;
    // public Joystick joystick;
    // public float speed;//移动速度
    // private bool isDeath;
    private void Start()
    {
        GameApp.instance.isDeath = false;
        rb = GetComponent<Rigidbody2D>();
        animator = gameObject.GetComponentInChildren<Animator>();
    }
    void Update()
    {
        //获取虚拟摇杆的位置
        float horizontal = 0;
        float vertical = 0;
        horizontal = Joystick.Instance.Horizontal;
        vertical = Joystick.Instance.Vertical;
        //判断单点触摸
        if (Input.touchCount == 1)
        {
            //获取触摸对象
            Touch touch = Input.touches[0];
            //获取触摸阶段
            switch (touch.phase)
            {
                case TouchPhase.Began://开始触摸
                    // horizontal = Joystick.Instance.Horizontal;
                    // vertical = Joystick.Instance.Vertical;
                    Debug.Log("开始触摸");
                    break;
                case TouchPhase.Moved://触摸移动
                    Debug.Log("触摸移动");
                    break;
                case TouchPhase.Stationary://触摸静止
                    Debug.Log("触摸静止");
                    break;
                case TouchPhase.Ended://触摸结束
                    horizontal = 0;
                    vertical = 0;
                    Debug.Log("触摸结束");
                    break;
                case TouchPhase.Canceled://触摸因为其他事件打断
                    Debug.Log("触摸因为其他事件打断");
                    break;
            }
        }

        if (horizontal == 0)
        {
            horizontal = Input.GetAxisRaw("Horizontal");
        }
        if (vertical == 0)
        {
            vertical = Input.GetAxisRaw("Vertical");
        }
        // horizontalInput = Input.GetAxisRaw("Horizontal");
        // verticalInput = Input.GetAxisRaw("Vertical");
        //控制动画
        if (horizontal == 0 && vertical == 0)
        {
            animator.SetBool("isRun", false);
        }
        else
        {
            animator.SetBool("isRun", true);
            animator.SetFloat("Horizontal", horizontal);
            animator.SetFloat("Vertical", vertical);
        }
        movement = new Vector2(horizontal, vertical).normalized;
    }

    private void FixedUpdate()
    {
        if (!GameApp.instance.isDeath)
        {
            //移动代码
            rb.MovePosition(rb.position + movement * GameApp.instance.speed * Time.fixedDeltaTime);
        }
    }

    //检测碰撞 玩家死亡
    private void OnTriggerEnter2D(Collider2D other)
    {
        if (other.gameObject.layer == LayerMask.NameToLayer("Explosion"))
        {
            GameOver();
        }
    }

    // 检测碰撞 玩家死亡
    private void OnCollisionEnter2D(Collision2D collision)
    {
        //碰到层级
        if (collision.gameObject.layer == LayerMask.NameToLayer("Enemy"))
        {
            GameOver();
            gameObject.GetComponent<Collider2D>().isTrigger = true;

        }
    }

    private void GameOver()
    {
        animator.SetTrigger("isDeath");//播放死亡动画
        AudioManager.Instance.PlaSound("Death");
        GameApp.instance.isDeath = true;
        Invoke(nameof(NewRound), 1f);
    }

    private void NewRound()
    {
        GameApp.instance.del();
        GameApp.instance.Init();
        //显示结束界面
        UIManager.Instance.ShowUI<GameOverUI>("结束界面");
        // SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);//重新开始场景
        //关闭虚拟界面
        
        //TODO:结束游戏
        Destroy(gameObject);
    }
}
